import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { getHomeDiscountData, getHomeGoodPriceData, getHomeHighScoreData, getHomeHotRecommendData, getHomeLongForData, getHomePlusData } from '@/services';

export const fetchHomeDataAction = createAsyncThunk(
  "fetchdata", 
  // async (payload, { dispatch })=>{
  //   // getHomeGoodPriceData().then(res=>{
  //   //   dispatch(res)
  //   // })
  //   // const res = await getHomeGoodPriceData()
  //   return await getHomeGoodPriceData()
  // }
  (payload, { dispatch }) => {
    getHomeGoodPriceData().then(res=>{
      dispatch(changeGoodPriceInfoAction(res))
    })
    getHomeHighScoreData().then(res => {
      dispatch(changeHighScoreInfoAction(res))
    })
    getHomeDiscountData().then(res => {
      dispatch(changeDicountInfoAction(res))
    })
    getHomeHotRecommendData().then(res => {
      dispatch(changeHotRecommendData(res))
    })
    getHomeLongForData().then(res => {
      dispatch(changeLongForData(res))
    })
    getHomePlusData().then(res => {
      dispatch(changePlusData(res))
    })
  }
)

const homeSlice = createSlice({
  name: 'home',
  initialState: {
    goodPriceInfo: {},
    highScoreInfo:{},
    discountInfo:{},
    hotRecommendInfo:{},
    longForInfo:{},
    plusInfo:{}
  },
  reducers: {
    changeGoodPriceInfoAction(state, { payload }) {
      state.goodPriceInfo = payload
    },
    changeHighScoreInfoAction(state, { payload }) {
      state.highScoreInfo = payload
    },
    changeDicountInfoAction(state, { payload }) {
      state.discountInfo = payload
    },
    changeHotRecommendData(state, { payload }) {
      state.hotRecommendInfo = payload
    },
    changeLongForData(state, { payload }) {
      state.longForInfo = payload
    },
    changePlusData(state, { payload }) {
      state.plusInfo = payload
    },
  },
  extraReducers:(builder)=>{
    // builder.addCase(fetchHomeDataAction.fulfilled, (state, {payload})=> {
    //   state.goodPriceInfo = payload
    // })
  }
})

export const { changeGoodPriceInfoAction, changeHighScoreInfoAction, changeDicountInfoAction, 
  changeHotRecommendData,
  changeLongForData,
  changePlusData
} = homeSlice.actions

export default homeSlice.reducer